using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Drawing;

namespace TSPLoader
{
    class FileLoader
    {
        private char[] delim;
        private string _path;
        //private Zone zone;
        
        private bool iszone;
        private StreamReader sr;
        public FileLoader(string path)
        {
            _path = path;
   
            iszone = false;
            delim = new char[] { ' ', '\t', ',' };
        }


        public void LoadFile(Zone [] zones, FileHeader fileheader)
        {
            string line = "";
            string prev = "";

            sr = new StreamReader(_path);

            FillZones(FillHeader(fileheader),zones);
        }

        private void FillZones(string p, Zone [] zones)
        {
            
            string[] values;
            string [] strs;
            int zoneID = 0;
            int i;

            

            do
            {
                zones[zoneID] = new Zone();
                strs = p.Split(delim, StringSplitOptions.RemoveEmptyEntries);
                
                getVariables(strs, zones[zoneID]);

                for(i=0;i<zones[zoneID].NumberOfNodes;i++)
                {
                    p = sr.ReadLine();
                    values = p.Split(delim, StringSplitOptions.RemoveEmptyEntries);
                    zones[zoneID].FillPointsLine(values);
                }
                for (i = 0; i < zones[zoneID].NumberOfElements; i++)
                {
                    p = sr.ReadLine();
                    values = p.Split(delim, StringSplitOptions.RemoveEmptyEntries);

                    zones[zoneID].FillTopology(values);
                }
                zoneID++;
            } while ((p=sr.ReadLine())!=null && p.StartsWith("ZONE"));       

            
        }

        private string FillHeader(FileHeader fileheader)
        {
            string line = "";
            string header = "";
            while ((line = sr.ReadLine())!=null)
            {
                if (line.StartsWith("ZONE"))
                {
                    if (header != "")
                    {
                        fileheader = new FileHeader();
                        parseHeader(header, fileheader);
                    }
                    break;
                }
                header += line;
            }
            return line;
        }

        private void parseHeader(string header, FileHeader fileheader)
        {

            if (header.StartsWith("TITLE"))
            {
                int start = header.IndexOf('\"')+1;
                int end = header.IndexOf('\"',start);
                FileHeader.Title = header.Substring(start, end - start) ;
                parseHeader(header.Remove(0, end+1), fileheader);

            }
            else if (header.StartsWith("VARIABLES"))
            {
                FileHeader.Variables = new List<string>();
                string lst = header.Split(new char[] { '=' }, StringSplitOptions.RemoveEmptyEntries)[1];
                string[] vars = lst.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                for (int i = 0; i < vars.Length; i++)
                {
                    string var = vars[i];
                    var = getContent(var);
                    
                    FileHeader.Variables.Add(var);
                }

            }
        }

        private void getVariables(string[] strs, Zone zone)
        {
            string [] temp;
            string var;
            foreach (string str in strs)
            {
                temp = str.Split(new char[] { '=' }, StringSplitOptions.RemoveEmptyEntries);
                var = temp[0].ToUpper();
                if (var == "T")
                {
                    zone.Title = temp[1];
                }
                if (var == "N")
                {
                    zone.NumberOfNodes = int.Parse(temp[1]);
                }
                if (var == "E")
                {
                    zone.NumberOfElements = int.Parse(temp[1]);
                }
                if (var == "ET")
                {
                    zone.ElementType = temp[1];
                }
                if (var == "C")
                {
                    zone.Color =  Color.FromName(temp[1]);
                }
                if (var == "F")
                {
                    zone.FEFormat = temp[1];
                }
                if (var == "D")
                {
                    zone.FEDuplist.AddRange(temp[1].Split(','));
                }
                if (var == "NV")
                {
                    zone.NodeValue = temp[1] ;
                }
                if (var == "DT")
                {
                    zone.DataType.AddRange(temp[1].Split(','));
                }

            }

        }
        private string getContent(string str)
        {
            if (str.StartsWith("\""))
                str.Remove(0, 1);
            if (str.EndsWith("\""))
                str.Remove(str.Length - 1, 1);
            return str;
        }
    }
}
